package com.zyj.myapplication.Activity;


import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.zyj.myapplication.Fragment.CIntroFragment;
import com.zyj.myapplication.Fragment.CClassFragment;
import com.zyj.myapplication.Fragment.CvideoFragment;
import com.zyj.myapplication.Model.ClubModel;
import com.zyj.myapplication.Model.Constant;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;

;import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClubInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mClub;
    private LinearLayout mClass;
    private LinearLayout mVideo;
    private ImageView CI_return, CI_collect, CI_photo;
    private TextView CI_name;
    private Fragment CI_jianjie;
    private Fragment CI_kecheng;
    private Fragment CI_video;
    private Context mContext;
    private String club_id;
    private int userid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_info);
        initViews();//初始化
        initEvents();//初始化事件
        showData();
        setSelect(0);
        getClubState();

    }

    private void showData() {
        Intent intent = getIntent();
        String club_name = intent.getStringExtra("club_name");
        String club_photo = intent.getStringExtra("club_photo");
        club_id = intent.getStringExtra("club_id");
        CI_name.setText(club_name);
        Picasso.with(mContext).load(Constant.PIC_URL+ club_photo).into(CI_photo);
    }


    private void initViews() {
        mClub = (LinearLayout) findViewById(R.id.clubinfo_club);
        mClass = (LinearLayout) findViewById(R.id.clubinfo_class);
        mVideo = (LinearLayout) findViewById(R.id.clubinfo_video);
        CI_return = (ImageView) findViewById(R.id.CI_return);
        CI_collect = (ImageView) findViewById(R.id.CI_collect);
        CI_photo = (ImageView) findViewById(R.id.CI_photo);
        CI_name = (TextView) findViewById(R.id.CI_name);
        userid = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
      //  Log.v("打印我的ID", String.valueOf(userid));
    }

    private void initEvents() {
        mClub.setOnClickListener(this);
        mClass.setOnClickListener(this);
        mVideo.setOnClickListener(this);
        CI_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        CI_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                if (CI_jianjie == null) {
                    CI_jianjie = new CIntroFragment();
                    transaction.add(R.id.id_content1, CI_jianjie);
                } else {
                    transaction.show(CI_jianjie);
                }
                break;
            case 1:
                if (CI_kecheng == null) {
                    CI_kecheng = new CClassFragment();
                    transaction.add(R.id.id_content1, CI_kecheng);
                } else {
                    transaction.show(CI_kecheng);

                }
                break;
            case 2:
                if (CI_video == null) {
                    CI_video = new CvideoFragment();
                    transaction.add(R.id.id_content1, CI_video);
                } else {
                    transaction.show(CI_video);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (CI_jianjie != null) {
            transaction.hide(CI_jianjie);
        }
        if (CI_kecheng != null) {
            transaction.hide(CI_kecheng);
        }
        if (CI_video != null) {
            transaction.hide(CI_video);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clubinfo_club:
                setSelect(0);
                break;
            case R.id.clubinfo_class:
                setSelect(1);
                break;
            case R.id.clubinfo_video:
                setSelect(2);
                break;
            default:
                break;
        }
    }

    private void collect(){
       ClubModel clubModel = new ClubModel();
        Call<Integer> call = clubModel.changeClubState(userid,club_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if(response.body()!=0){
                    Toast.makeText(ClubInfoActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }

    private void getClubState(){
        ClubModel clubModel = new ClubModel();
        Call<Integer> call = clubModel.getClubState(club_id,userid);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    CI_collect.setImageResource(R.drawable.collect2);
                }else{
                    CI_collect.setImageResource(R.drawable.collect1);
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
}

